package org.Yiran.timetale_re.worldgen;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.data.worldgen.placement.PlacementUtils;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BiomeTags;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.minecraftforge.common.world.BiomeModifier;
import net.minecraftforge.common.world.ModifiableBiomeInfo;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import org.Yiran.timetale_re.Timetale_re;

public class ModBiomeModifierRegister {
    
    public static final DeferredRegister<Codec<? extends BiomeModifier>> BIOME_MODIFIER_SERIALIZERS =
            DeferredRegister.create(ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, Timetale_re.MODID);

    public static final RegistryObject<Codec<ModOreBiomeModifier>> ORE_MODIFIER_CODEC = BIOME_MODIFIER_SERIALIZERS.register(
            "ore_modifier", () -> ModOreBiomeModifier.CODEC
    );
    
    // 银矿石生物群系修饰符
    public static final ResourceKey<BiomeModifier> SILVER_ORE_MODIFIER = createKey("silver_ore");
    public static final ResourceKey<BiomeModifier> DEEPSLATE_SILVER_ORE_MODIFIER = createKey("deepslate_silver_ore");
    
    // 铝矿石生物群系修饰符
    public static final ResourceKey<BiomeModifier> ALUMINUM_ORE_MODIFIER = createKey("aluminum_ore");
    public static final ResourceKey<BiomeModifier> DEEPSLATE_ALUMINUM_ORE_MODIFIER = createKey("deepslate_aluminum_ore");
    
    // 钛矿石生物群系修饰符
    public static final ResourceKey<BiomeModifier> TITANIUM_ORE_MODIFIER = createKey("titanium_ore");
    public static final ResourceKey<BiomeModifier> DEEPSLATE_TITANIUM_ORE_MODIFIER = createKey("deepslate_titanium_ore");
    
    // 水晶簇生物群系修饰符
    public static final ResourceKey<BiomeModifier> CRYSTAL_CLUSTER_MODIFIER = createKey("crystal_cluster");
    
    // 能晶簇生物群系修饰符
    public static final ResourceKey<BiomeModifier> OVERWORLD_ENERGY_CRYSTAL_CLUSTER_MODIFIER = createKey("overworld_energy_crystal_cluster");
    public static final ResourceKey<BiomeModifier> NETHER_ENERGY_CRYSTAL_CLUSTER_MODIFIER = createKey("nether_energy_crystal_cluster");

    public record ModOreBiomeModifier(HolderSet<Biome> biomes, Holder<PlacedFeature> feature,
                                     GenerationStep.Decoration step) implements BiomeModifier {

        public static final Codec<ModOreBiomeModifier> CODEC = RecordCodecBuilder.create(builder -> builder.group(
                Biome.LIST_CODEC.fieldOf("biomes").forGetter(ModOreBiomeModifier::biomes),
                PlacedFeature.CODEC.fieldOf("feature").forGetter(ModOreBiomeModifier::feature),
                GenerationStep.Decoration.CODEC.fieldOf("step").forGetter(ModOreBiomeModifier::step)
        ).apply(builder, ModOreBiomeModifier::new));

        @Override
        public void modify(Holder<Biome> biome, Phase phase, ModifiableBiomeInfo.BiomeInfo.Builder builder) {
            if (phase == Phase.ADD && this.biomes.contains(biome)) {
                builder.getGenerationSettings().addFeature(this.step, this.feature);
            }
        }

        @Override
        public Codec<? extends BiomeModifier> codec() {
            return CODEC;
        }
    }
    
    /**
     * 创建生物群系修饰符注册键
     * <p>
     * 创建一个用于注册生物群系修饰符的资源键
     * </p>
     *
     * @param name 修饰符名称
     * @return 生物群系修饰符的资源键
     */
    public static ResourceKey<BiomeModifier> createKey(String name) {
        return ResourceKey.create(ForgeRegistries.Keys.BIOME_MODIFIERS, ResourceLocation.fromNamespaceAndPath(Timetale_re.MODID, name));
    }
    
    /**
     * 引导生物群系修饰符
     * <p>
     * 用于数据生成的引导方法
     * </p>
     *
     * @param context 引导上下文
     */
    public static void bootstrap(BootstapContext<BiomeModifier> context) {
        HolderGetter<PlacedFeature> placedFeatures = context.lookup(Registries.PLACED_FEATURE);
        HolderGetter<Biome> biomes = context.lookup(Registries.BIOME);
        
        // 注册银矿石生物群系修饰符
        Holder<PlacedFeature> silverOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_SILVER_ORE_PLACED_KEY);
        Holder<PlacedFeature> deepslateSilverOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_DEEPSLATE_SILVER_ORE_PLACED_KEY);
        HolderSet<Biome> overworldBiomes = biomes.getOrThrow(BiomeTags.IS_OVERWORLD);
        
        context.register(SILVER_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, silverOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        context.register(DEEPSLATE_SILVER_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, deepslateSilverOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        
        // 注册铝矿石生物群系修饰符
        Holder<PlacedFeature> aluminumOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_ALUMINUM_ORE_PLACED_KEY);
        Holder<PlacedFeature> deepslateAluminumOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_DEEPSLATE_ALUMINUM_ORE_PLACED_KEY);
        
        context.register(ALUMINUM_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, aluminumOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        context.register(DEEPSLATE_ALUMINUM_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, deepslateAluminumOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        
        // 注册钛矿石生物群系修饰符
        Holder<PlacedFeature> titaniumOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_TITANIUM_ORE_PLACED_KEY);
        Holder<PlacedFeature> deepslateTitaniumOreHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_DEEPSLATE_TITANIUM_ORE_PLACED_KEY);
        
        context.register(TITANIUM_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, titaniumOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        context.register(DEEPSLATE_TITANIUM_ORE_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, deepslateTitaniumOreHolder, GenerationStep.Decoration.UNDERGROUND_ORES));
        
        // 注册水晶簇生物群系修饰符
        Holder<PlacedFeature> crystalClusterHolder = placedFeatures.getOrThrow(ModPlacedFeatures.CRYSTAL_CLUSTER_PLACED_KEY);
        
        context.register(CRYSTAL_CLUSTER_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, crystalClusterHolder, GenerationStep.Decoration.UNDERGROUND_DECORATION));
        
        // 注册能晶簇生物群系修饰符 - 主世界和下界分别注册
        Holder<PlacedFeature> overworldEnergyCrystalClusterHolder = placedFeatures.getOrThrow(ModPlacedFeatures.OVERWORLD_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY);
        Holder<PlacedFeature> netherEnergyCrystalClusterHolder = placedFeatures.getOrThrow(ModPlacedFeatures.NETHER_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY);
        HolderSet<Biome> netherBiomes = biomes.getOrThrow(BiomeTags.IS_NETHER);
        
        context.register(OVERWORLD_ENERGY_CRYSTAL_CLUSTER_MODIFIER, 
            new ModOreBiomeModifier(overworldBiomes, overworldEnergyCrystalClusterHolder, GenerationStep.Decoration.UNDERGROUND_DECORATION));
        context.register(NETHER_ENERGY_CRYSTAL_CLUSTER_MODIFIER, 
            new ModOreBiomeModifier(netherBiomes, netherEnergyCrystalClusterHolder, GenerationStep.Decoration.UNDERGROUND_DECORATION));
    }
}